import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MainUI extends JFrame
        implements ActionListener {
    private static final long serialVersionUID = 1L;
    public static final String GRAY_CMD = "灰度变换";
    public static final String BINARY_CMD = "图像二值化";
    public static final String GAMMA_CMD = "幂次变换";
    public static final String SHARPENING_CMD = "锐化";
    public static final String HISTOGRAM_CMD = "直方图均衡";
    public static final String MEAN_CMD = "均值滤波";
    public static final String MEDIAN_CMD = "中值滤波";
    public static final String SOBEL_CMD = "Sobel";
    public static final String PREWITT_CMD = "Prewitt";
    public static final String IMAGE_CMD = "选择图像...";
    public static final String SAVE_CMD = "保存";

    private JButton grayBtn;
    private JButton binaryBtn;
    private JButton gammaBtn;
    private JButton sharpeningBtn;
    private JButton histogramBtn;
    private JButton meanBtn;
    private JButton medianBtn;
    private JButton sobelBtn;
    private JButton prewittBtn;
    private JButton imgBtn;
    private JButton saveBtn;
    private ImagePanel imagePanel;

    // image
    private BufferedImage srcImage;

    public MainUI() {
        setTitle("WeStudio2021");
        grayBtn = new JButton(GRAY_CMD);
        binaryBtn = new JButton(BINARY_CMD);
        gammaBtn = new JButton(GAMMA_CMD);
        sharpeningBtn = new JButton(SHARPENING_CMD);
        histogramBtn = new JButton(HISTOGRAM_CMD);
        meanBtn = new JButton(MEAN_CMD);
        medianBtn = new JButton(MEDIAN_CMD);
        sobelBtn = new JButton(SOBEL_CMD);
        prewittBtn = new JButton(PREWITT_CMD);
        imgBtn = new JButton(IMAGE_CMD);
        saveBtn = new JButton(SAVE_CMD);

        // buttons
        JPanel bthPanelNorth = new JPanel();
        //?
        bthPanelNorth.setLayout(new FlowLayout(FlowLayout.LEFT));
        JPanel bthPanelSouth = new JPanel();
        bthPanelNorth.add(grayBtn);
        bthPanelNorth.add(binaryBtn);
        bthPanelNorth.add(gammaBtn);
        bthPanelNorth.add(sharpeningBtn);
        bthPanelNorth.add(histogramBtn);
        bthPanelNorth.add(meanBtn);
        bthPanelNorth.add(medianBtn);
        bthPanelNorth.add(sobelBtn);
        bthPanelNorth.add(prewittBtn);
        bthPanelSouth.add(imgBtn);
        bthPanelSouth.add(saveBtn);

        // filters
        imagePanel = new ImagePanel();

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(bthPanelNorth, BorderLayout.NORTH);
        getContentPane().add(imagePanel, BorderLayout.CENTER);
        getContentPane().add(bthPanelSouth, BorderLayout.SOUTH);




        // setup listener

        setupActionListener();
    }

    private void setupActionListener() {
        grayBtn.addActionListener(this);
        binaryBtn.addActionListener(this);
        gammaBtn.addActionListener(this);
        sharpeningBtn.addActionListener(this);
        histogramBtn.addActionListener(this);
        meanBtn.addActionListener(this);
        medianBtn.addActionListener(this);
        sobelBtn.addActionListener(this);
        prewittBtn.addActionListener(this);
        imgBtn.addActionListener(this);
        saveBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(SwingUtilities.isEventDispatchThread()) {
            System.out.println("Event Dispath Thread!!");
        }

        if(srcImage == null)
        {
            JOptionPane.showMessageDialog(this, "请先选择图像源文件");
            try {
                JFileChooser chooser = new JFileChooser();
                setFileTypeFilter(chooser);
                chooser.showOpenDialog(null);
                File f = chooser.getSelectedFile();
                if(f != null) {
                    srcImage = ImageIO.read(f);
                    imagePanel.setSourceImage(srcImage);
                    imagePanel.repaint();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return;
        }
        if(IMAGE_CMD.equals(e.getActionCommand())) {
            try {
                JFileChooser chooser = new JFileChooser();
                setFileTypeFilter(chooser);
                chooser.showOpenDialog(null);
                File f = chooser.getSelectedFile();
                if(f != null) {
                    srcImage = ImageIO.read(f);
                    imagePanel.setSourceImage(srcImage);
                    imagePanel.repaint();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            imagePanel.repaint();
        }

        if (GRAY_CMD.equals(e.getActionCommand())) {
            imagePanel.grayFilter();
            imagePanel.repaint();
        }
        if (BINARY_CMD.equals(e.getActionCommand())) {
            imagePanel.binaryFilter();
            imagePanel.repaint();
        }
        if (GAMMA_CMD.equals(e.getActionCommand())) {
            imagePanel.gammaFilter();
            imagePanel.repaint();
        }
        if (MEAN_CMD.equals(e.getActionCommand())) {
            imagePanel.meanFilter();
            imagePanel.repaint();
        }
        if (MEDIAN_CMD.equals(e.getActionCommand())) {
            imagePanel.medianFilter();
            imagePanel.repaint();
        }
        if (SHARPENING_CMD.equals(e.getActionCommand())) {
            imagePanel.sharpenFilter();
            imagePanel.repaint();
        }
        if (HISTOGRAM_CMD.equals(e.getActionCommand())) {
            imagePanel.histogramFilter();
            imagePanel.repaint();
        }
        if (SOBEL_CMD.equals(e.getActionCommand())) {
            imagePanel.sobelFilter();
            imagePanel.repaint();
        }
        if (PREWITT_CMD.equals(e.getActionCommand())) {
            imagePanel.prewittFilter();
            imagePanel.repaint();
        }
        else if (SAVE_CMD.equals(e.getActionCommand())) {
            try {
                BufferedImage imageRes = imagePanel.processOut();
                imagePanel.saveImageFile(imageRes);
                imagePanel.repaint();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            //return;
        }
    }

    public void setFileTypeFilter(JFileChooser chooser) {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "JPG & PNG Images", "jpg", "png");
        chooser.setFileFilter(filter);
    }

    public void openView() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(1600, 800));
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        MainUI ui = new MainUI();
        ui.openView();
    }
}
